package net.jonbuck.tassoo.ui.view.page;

import java.util.Date;
import java.util.Formatter;

import net.jonbuck.tassoo.model.Recurrence;
import net.jonbuck.tassoo.model.Task;
import net.jonbuck.tassoo.model.impl.TassooFactoryImpl;
import net.jonbuck.tassoo.ui.nl.Messages;
import net.jonbuck.tassoo.ui.util.CalendarComboWidgetValueProperty;
import net.jonbuck.tassoo.ui.util.CommonImages;
import net.jonbuck.tassoo.ui.util.DateFieldHelper;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * 
 * 
 * @since 1.0.0
 */
public class TaskRecurrenceTab extends Composite {

	/** ... TODO Javadoc ... */
	protected static final int DAILY = 0;

	/** ... TODO Javadoc ... */
	protected static final int MONTHLY = 1;

	/** ... TODO Javadoc ... */
	protected static final int WEEKLY = 2;

	/** ... TODO Javadoc ... */
	protected static final int YEARLY = 3;

	/** ... TODO Javadoc ... */
	private Button btnCreateTask;

	/** ... TODO Javadoc ... */
	private Button btnFriday;

	/** ... TODO Javadoc ... */
	private Button btnMonday;

	/** ... TODO Javadoc ... */
	private Button btnResetTask;

	/** ... TODO Javadoc ... */
	private Button btnSaturday;

	/** ... TODO Javadoc ... */
	private Button btnSunday;

	/** ... TODO Javadoc ... */
	private Button btnTaskRecurs;

	/** ... TODO Javadoc ... */
	private Button btnThursday;

	/** ... TODO Javadoc ... */
	private Button btnTuesday;

	/** ... TODO Javadoc ... */
	private Button btnWednesday;

	/** ... TODO Javadoc ... */
	private Button dailyButton;

	/** ... TODO Javadoc ... */
	private CalendarCombo endDate;

	/** ... TODO Javadoc ... */
	private Spinner everyDailySpinner;

	/** ... TODO Javadoc ... */
	private Spinner everyWeekSpinner;

	/** .. TODO Javadoc ... */
	private GridLayout gl_dailyPattern;

	/** ... TODO Javadoc ... */
	private GridLayout gl_monthlyPattern;

	/** ... TODO Javadoc ... */
	private GridLayout gl_weeklyPattern;

	/** ... TODO Javadoc ... */
	private GridLayout gl_yearlyPattern;

	/** ... TODO Javadoc ... */
	private GridData ld_dailyPattern;

	/** ... TODO Javadoc ... */
	private GridData ld_monthlyPattern;

	/** ... TODO Javadoc ... */
	private GridData ld_weeklyPattern;

	/** ... TODO Javadoc ... */
	private GridData ld_yearlyPattern;

	/** ... TODO Javadoc ... */
	private Button monthlyButton;

	/** ... TODO Javadoc ... */
	private Section patternSection;

	/** ... TODO Javadoc ... */
	private int position;

	/** ... TODO Javadoc ... */
	private Form recurrenceForm;

	/** ... TODO Javadoc ... */
	private CalendarCombo startDate;

	/** ... TODO Javadoc ... */
	private Task task;

	/** ... TODO Javadoc ... */
	private String TYPE = "pattern_type"; //$NON-NLS-1$

	/** ... TODO Javadoc ... */
	private Button weeklyButton;

	/** ... TODO Javadoc ... */
	private Button yearlyButton;

	/** ... TODO Javadoc ... */
	private Composite bodyComposite;

	/** ... TODO Javadoc ... */
	private Composite patternsComposite;

	private UpdateValueStrategy startDateModelToTargetStratgey = new UpdateValueStrategy() {
		public UpdateValueStrategy setConverter(IConverter converter) {
			this.converter = new IConverter() {
				public Object getFromType() {
					return Date.class;
				}

				public Object getToType() {
					return Date.class;
				}

				public Object convert(Object fromObject) {
					if (fromObject == null) {
						return new Date();
					}
					return fromObject;
				}
			};
			return this;
		}

	};

	/**
	 * 
	 * @param parent
	 * @param form
	 * @param position
	 */
	public TaskRecurrenceTab(Composite parent, Form form, int position) {
		super(parent, SWT.NONE);
		this.recurrenceForm = form;
		this.position = position;
		init();
	}

	/**
	 * 
	 * @param toolkit
	 * @param actionAndRange
	 */
	private void createAction(FormToolkit toolkit, Composite actionAndRange) {
		Section actionSection = toolkit.createSection(actionAndRange, Section.TITLE_BAR);
		actionSection.setLayout(new GridLayout());
		actionSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		actionSection.setText("Action");

		Composite actionContents = toolkit.createComposite(actionSection);
		actionContents.setLayout(new GridLayout());
		actionContents.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		btnResetTask = toolkit.createButton(actionContents, "Reset the task", SWT.RADIO);
		btnResetTask.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		btnCreateTask = toolkit.createButton(actionContents, "Create a new task", SWT.RADIO);
		btnCreateTask.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		actionSection.setClient(actionContents);
	}

	/**
	 * 
	 * @param toolkit
	 * @param bodyComposite
	 */
	private void createActionAndRangeSection(FormToolkit toolkit, Composite bodyComposite) {
		createRange(toolkit, bodyComposite);
		createAction(toolkit, bodyComposite);
	}

	/**
	 * 
	 * @param parent
	 */
	private void createControl(Composite parent) {

		FormToolkit toolkit = new FormToolkit(parent.getDisplay());

		bodyComposite = toolkit.createComposite(parent);
		bodyComposite.setLayout(new GridLayout(3, true));
		bodyComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		btnTaskRecurs = new Button(bodyComposite, SWT.CHECK);
		btnTaskRecurs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
		btnTaskRecurs.setText("This task does not recur");
		btnTaskRecurs.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (btnTaskRecurs.getSelection()) {
					activateControl(bodyComposite, false);
				} else {
					if (task.getRecurrenceSettings() == null) {
						task.setRecurrenceIndicator(true);
						task.setRecurrenceSettings(TassooFactoryImpl.eINSTANCE.createRecurrence());
						initDataBindings();
					}
					activateControl(bodyComposite, true);
				}
			}
		});

		createSequenceSection(toolkit, bodyComposite);
		createPatternSection(toolkit, bodyComposite);
		createActionAndRangeSection(toolkit, bodyComposite);

		toolkit.dispose();
	}

	/**
	 * 
	 * @param b
	 */
	protected void activateControl(Control control, boolean b) {
		if (control instanceof Composite && ((Composite) control).getChildren() != null) {
			Composite composite = (Composite) control;
			for (Control subControl : composite.getChildren()) {
				activateControl(subControl, b);
			}
			if (composite != bodyComposite) {
				composite.setEnabled(b);
			}
		} else if (control != btnTaskRecurs) {
			control.setEnabled(b);
		}
	}

	/**
	 * 
	 * @param toolkit
	 * @param patternSection
	 */
	private void createDailyPattern(FormToolkit toolkit, Composite patternsComposite) {
		Group dailyPattern = new Group(patternsComposite, SWT.NONE);
		dailyPattern.setData(TYPE, DAILY);
		dailyPattern.setText(Messages.taskrecurrencesection_dailypattern_title);
		dailyPattern.setForeground(dailyPattern.getDisplay().getSystemColor(SWT.COLOR_GRAY));
		gl_dailyPattern = new GridLayout(3, false);
		dailyPattern.setLayout(gl_dailyPattern);
		ld_dailyPattern = new GridData(SWT.FILL, SWT.FILL, true, true);
		dailyPattern.setLayoutData(ld_dailyPattern);

		Label lblDescription = new Label(dailyPattern, SWT.NONE | SWT.WRAP);
		lblDescription.setText(Messages.taskrecurrencesection_dailypattern_description);
		lblDescription.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));

		Button btnEvery = new Button(dailyPattern, SWT.RADIO);
		btnEvery.setText("Every:");
		btnEvery.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {

			}
		});
		btnEvery.setSelection(true);

		everyDailySpinner = new Spinner(dailyPattern, SWT.BORDER);
		GridData ld_everyDailySpinner = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
		ld_everyDailySpinner.widthHint = 40;
		everyDailySpinner.setLayoutData(ld_everyDailySpinner);
		everyDailySpinner.setMaximum(364);
		everyDailySpinner.setMinimum(1);
		everyDailySpinner.setIncrement(1);

		Label lblDays = new Label(dailyPattern, SWT.NONE);
		lblDays.setText("days");

		Button btnWeekday = new Button(dailyPattern, SWT.RADIO);
		btnWeekday.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 3, 1));
		btnWeekday.setText("Every Weekday");

		Button btnWeekend = new Button(dailyPattern, SWT.RADIO);
		btnWeekend.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 3, 1));
		btnWeekend.setText("Every Weekend day");

		dailyPattern.getParent().layout();
	}

	/**
	 * 
	 * @param toolkit
	 * @param patternsComposite
	 */
	private void createMonthlyPattern(FormToolkit toolkit, Composite patternsComposite) {
		Group monthlyPattern = new Group(patternsComposite, SWT.NONE);
		monthlyPattern.setData(TYPE, MONTHLY);
		monthlyPattern.setText(Messages.taskrecurrencesection_monthlypattern_title);
		gl_monthlyPattern = new GridLayout();
		monthlyPattern.setLayout(gl_monthlyPattern);
		ld_monthlyPattern = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
		ld_monthlyPattern.exclude = true;
		monthlyPattern.setLayoutData(ld_monthlyPattern);
		monthlyPattern.setVisible(false);

		Composite monthSelection = toolkit.createComposite(monthlyPattern);
		monthSelection.setLayout(new GridLayout(3, false));
		monthSelection.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		Label lblEvery = new Label(monthSelection, SWT.RADIO);
		lblEvery.setText("Every:");

		everyWeekSpinner = new Spinner(monthSelection, SWT.BORDER);
		GridData ld_everyWeeklySpinner = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
		ld_everyWeeklySpinner.widthHint = 40;
		everyWeekSpinner.setLayoutData(ld_everyWeeklySpinner);
		everyWeekSpinner.setMaximum(11);
		everyWeekSpinner.setMinimum(1);
		everyWeekSpinner.setIncrement(1);

		Label lblDays = new Label(monthSelection, SWT.NONE);
		lblDays.setText("month(s) on the:");

		Composite daySelection = toolkit.createComposite(monthlyPattern);
		GridLayout gl_daySelection = new GridLayout(8, false);
		gl_daySelection.horizontalSpacing = 8;
		daySelection.setLayout(gl_daySelection);
		daySelection.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		GridData gd_button = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		gd_button.widthHint = 30;

		for (int i = 1; i < 32; i++) {
			Button button = new Button(daySelection, SWT.CHECK);
			button.setLayoutData(gd_button);
			button.setText(new Formatter().format("%02d", Integer.valueOf(i)).toString());
		}

		Composite weekendAction = toolkit.createComposite(monthlyPattern);
		weekendAction.setLayout(new GridLayout());
		weekendAction.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		Label lblWhatToDo = new Label(weekendAction, SWT.NONE);
		lblWhatToDo.setText("If the date doesn't exist or occurs on a weekend:");

		Combo cmbWhatToDo = new Combo(weekendAction, SWT.BORDER);
		cmbWhatToDo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		cmbWhatToDo.setItems(new String[] { "Don't Move", "Move to Friday", "Move to Monday" });

		monthlyPattern.getParent().layout();
	}

	/**
	 * 
	 * @param toolkit
	 * @param bodyComposite
	 */
	private void createPatternSection(FormToolkit toolkit, Composite bodyComposite) {
		patternSection = toolkit.createSection(bodyComposite, Section.TITLE_BAR);
		GridLayout gl_patternsSection = new GridLayout();
		gl_patternsSection.horizontalSpacing = 0;
		gl_patternsSection.marginBottom = 0;
		gl_patternsSection.marginHeight = 0;
		gl_patternsSection.marginLeft = 0;
		gl_patternsSection.marginRight = 0;
		gl_patternsSection.marginTop = 0;
		gl_patternsSection.marginWidth = 0;
		gl_patternsSection.verticalSpacing = 0;
		patternSection.setLayout(gl_patternsSection);
		patternSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
		patternSection.setText("Pattern");

		patternsComposite = toolkit.createComposite(patternSection);
		GridLayout gl_patternsComposite = new GridLayout();
		gl_patternsComposite.horizontalSpacing = 0;
		gl_patternsComposite.marginBottom = 0;
		gl_patternsComposite.marginHeight = 0;
		gl_patternsComposite.marginLeft = 0;
		gl_patternsComposite.marginRight = 0;
		gl_patternsComposite.marginTop = 0;
		gl_patternsComposite.marginWidth = 0;
		gl_patternsComposite.verticalSpacing = 0;
		patternsComposite.setLayout(gl_patternsComposite);
		patternsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		createDailyPattern(toolkit, patternsComposite);
		createWeeklyPattern(toolkit, patternsComposite);
		createMonthlyPattern(toolkit, patternsComposite);
		createYearlyPattern(toolkit, patternsComposite);

		patternSection.setClient(patternsComposite);
	}

	/**
	 * 
	 * @param toolkit
	 * @param actionAndRange
	 */
	private void createRange(FormToolkit toolkit, Composite actionAndRange) {
		Section rangeSection = toolkit.createSection(actionAndRange, Section.TITLE_BAR);
		rangeSection.setLayout(new GridLayout());
		rangeSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		rangeSection.setText("Range");

		Composite rangeContents = toolkit.createComposite(rangeSection);
		rangeContents.setLayout(new GridLayout(3, false));
		rangeContents.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, false, false));

		toolkit.createLabel(rangeContents, "Starting:");

		startDate = new CalendarCombo(rangeContents, SWT.BORDER | SWT.READ_ONLY);
		startDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		final Button startDateHelper = new Button(rangeContents, SWT.PUSH);
		startDateHelper.setImage(CommonImages.getImage(CommonImages.CALENDAR));
		startDateHelper.addSelectionListener(new DateFieldHelper(startDate, startDateHelper));

		Button endingOn = new Button(rangeContents, SWT.CHECK);
		endingOn.setText("Ending:");

		endDate = new CalendarCombo(rangeContents, SWT.BORDER | SWT.READ_ONLY);
		endDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		final Button endDateHelper = new Button(rangeContents, SWT.PUSH);
		endDateHelper.setImage(CommonImages.getImage(CommonImages.CALENDAR));
		endDateHelper.addSelectionListener(new DateFieldHelper(endDate, endDateHelper));

		rangeSection.setClient(rangeContents);
	}

	/**
	 * 
	 * @param toolkit
	 * @param bodyComposite
	 */
	private void createSequenceSection(FormToolkit toolkit, Composite bodyComposite) {
		Section sequenceSection = toolkit.createSection(bodyComposite, Section.TITLE_BAR);
		sequenceSection.setLayout(new GridLayout());
		sequenceSection.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
		sequenceSection.setText("Sequence");

		Composite sequenceOptions = toolkit.createComposite(sequenceSection);
		sequenceOptions.setLayout(new GridLayout());

		dailyButton = new Button(sequenceOptions, SWT.RADIO);
		dailyButton.setText("Daily");
		dailyButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				showPattern(DAILY);
			}
		});
		dailyButton.setSelection(true);

		weeklyButton = new Button(sequenceOptions, SWT.RADIO);
		weeklyButton.setText("Weekly");
		weeklyButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				showPattern(WEEKLY);
			}
		});

		monthlyButton = new Button(sequenceOptions, SWT.RADIO);
		monthlyButton.setText("Monthly");
		monthlyButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				showPattern(MONTHLY);
			}
		});

		yearlyButton = new Button(sequenceOptions, SWT.RADIO);
		yearlyButton.setText("Yearly");
		yearlyButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				showPattern(YEARLY);
			}
		});

		sequenceSection.setClient(sequenceOptions);
	}

	/**
	 * 
	 * @param toolkit
	 * @param patternSection2
	 */
	private void createWeeklyPattern(FormToolkit toolkit, Composite patternsComposite) {
		Group weeklyPattern = new Group(patternsComposite, SWT.NONE);
		weeklyPattern.setData(TYPE, WEEKLY);
		weeklyPattern.setText(Messages.taskrecurrencesection_weeklypattern_title);
		gl_weeklyPattern = new GridLayout();
		gl_weeklyPattern.verticalSpacing = 10;
		weeklyPattern.setLayout(gl_weeklyPattern);
		ld_weeklyPattern = new GridData(SWT.FILL, SWT.FILL, true, true);
		ld_weeklyPattern.exclude = true;
		weeklyPattern.setLayoutData(ld_weeklyPattern);
		weeklyPattern.setVisible(false);

		Composite weekSelection = toolkit.createComposite(weeklyPattern);
		weekSelection.setLayout(new GridLayout(3, false));
		weekSelection.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		Label lblEvery = new Label(weekSelection, SWT.RADIO);
		lblEvery.setText("Every:");

		everyWeekSpinner = new Spinner(weekSelection, SWT.BORDER);
		GridData ld_everyWeeklySpinner = new GridData(SWT.BEGINNING, SWT.CENTER, false, false);
		ld_everyWeeklySpinner.widthHint = 40;
		everyWeekSpinner.setLayoutData(ld_everyWeeklySpinner);
		everyWeekSpinner.setMaximum(51);
		everyWeekSpinner.setMinimum(1);
		everyWeekSpinner.setIncrement(1);

		Label lblDays = new Label(weekSelection, SWT.NONE);
		lblDays.setText("week(s) on:");

		Composite daySelection = toolkit.createComposite(weeklyPattern);
		daySelection.setLayout(new GridLayout(4, false));
		daySelection.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

		btnMonday = new Button(daySelection, SWT.CHECK);
		btnMonday.setText("Monday");

		btnTuesday = new Button(daySelection, SWT.CHECK);
		btnTuesday.setText("Tuesday");

		btnWednesday = new Button(daySelection, SWT.CHECK);
		btnWednesday.setText("Wednesday");

		btnThursday = new Button(daySelection, SWT.CHECK);
		btnThursday.setText("Thursday");

		btnFriday = new Button(daySelection, SWT.CHECK);
		btnFriday.setText("Friday");

		btnSaturday = new Button(daySelection, SWT.CHECK);
		btnSaturday.setText("Saturday");

		btnSunday = new Button(daySelection, SWT.CHECK);
		btnSunday.setText("Sunday");

		weeklyPattern.getParent().layout();
	}

	/**
	 * 
	 * @param toolkit
	 * @param patternsComposite
	 */
	private void createYearlyPattern(FormToolkit toolkit, Composite patternsComposite) {
		Group yearlyPattern = new Group(patternsComposite, SWT.NONE);
		yearlyPattern.setData(TYPE, YEARLY);
		yearlyPattern.setText(Messages.taskrecurrencesection_yearlypattern_title);
		gl_yearlyPattern = new GridLayout();
		yearlyPattern.setLayout(gl_yearlyPattern);
		ld_yearlyPattern = new GridData(SWT.FILL, SWT.FILL, true, true);
		ld_yearlyPattern.exclude = true;
		yearlyPattern.setLayoutData(ld_yearlyPattern);
		yearlyPattern.setVisible(false);

		// TODO pattern design here

		yearlyPattern.getParent().layout();
	}

	private void init() {
		CTabFolder parentFolder = (CTabFolder) this.getParent();
		parentFolder.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				CTabFolder folder = (CTabFolder) e.widget;
				if (folder.getSelectionIndex() == position) {
					FormToolkit toolkit = new FormToolkit(getDisplay());
					recurrenceForm.getToolBarManager().removeAll();
					recurrenceForm.updateToolBar();
					toolkit.decorateFormHeading(recurrenceForm);
					toolkit.dispose();
				}
			}
		});

		GridLayout gl = new GridLayout();
		gl.horizontalSpacing = 0;
		gl.verticalSpacing = 0;
		gl.marginWidth = 0;
		gl.marginHeight = 0;
		setLayout(gl);

		createControl(this);

	}

	/**
	 * 
	 * @return
	 */
	private void initDataBindings() {
		DataBindingContext m_bindingContext = new DataBindingContext();

		IObservableValue categoryObserveWidget = WidgetProperties.selection().observe(btnTaskRecurs);
		IObservableValue categoryObserveValue = BeanProperties.value(Task.class, "recurrenceIndicator").observe(task);
		m_bindingContext.bindValue(categoryObserveWidget, categoryObserveValue, null, null);

		IObservableValue startDateObserveWidget = new CalendarComboWidgetValueProperty().observe(startDate);
		IObservableValue startDateObserveValue = BeanProperties.value(Recurrence.class, "startDate").observe(
				task.getRecurrenceSettings());
		m_bindingContext.bindValue(startDateObserveWidget, startDateObserveValue, startDateModelToTargetStratgey, null);
	}

	/**
	 * 
	 */
	public void setInput(Task task) {
		this.task = (Task) task;
		initDataBindings();
		activateControl(bodyComposite, !btnTaskRecurs.getSelection());
	}

	/**
	 * 
	 * @param pattern
	 */
	protected void showPattern(int pattern) {
		for (Control control : patternsComposite.getChildren()) {
			if (((Integer) control.getData(TYPE)) == pattern) {
				((GridData) control.getLayoutData()).exclude = false;
				control.setVisible(true);
			} else {
				((GridData) control.getLayoutData()).exclude = true;
				control.setVisible(false);
			}
		}
		patternSection.layout(true);
	}
}
